package service

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"smart-sensor-cloud/internal/api"
	"smart-sensor-cloud/pkg/util"
)

type (
	SectionRequest struct {
		Name string `json:"name"`
		Type string `json:"type"`
	}
)

func GetSections(ctx *gin.Context) {
	req := new(SectionRequest)
	if err := ctx.MustBindWith(req, binding.JSON); err != nil {
		ctx.Set("error", util.NewInvalidParameterError(err.Error()))
		return
	}

	client := ctx.MustGet("api").(*api.Client)

	result, err := client.SearchVillages(&api.SearchVillagesReq{
		Name: req.Name,
		Type: req.Type,
	})

	if err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OKWithPayload(result.Data.Content))
}
